//
//  WBMainViewController.swift
//  AdaoWeibo
//
//  Created by 孙道慧 on 2017/3/31.
//  Copyright © 2017年 maihaoche. All rights reserved.
//

import UIKit

class WBMainViewController: UITabBarController {

    // MARK: - 私有控件
    // 撰写按钮
    lazy var composeBtn : UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setUpChildControllers()
        setUpComposeBtn()
    }
    
    // 撰写按钮点击事件
    func composeBtnStatus() -> () {
        print("撰写按钮被点击。。。")
    }
    
}

    // extension 类似于oc中的分类，在swift中还用来切分代码块
    // 可以把相近的函数，放在同一个extension中，便于代码维护
    // 注意：和oc分类一样，extension中不能添加属性 只能添加方法
    // MARK: -  设置界面
    extension WBMainViewController {
        // 设置所有子控制器
         func setUpChildControllers() {
            let array = [
                ["clsName":"WBHomeViewController","title":"首页","imageName":"home"],
                ["clsName":"WBMessageViewController","title":"消息","imageName":"message_center"],
                ["clsName":"","title":"首页","imageName":""],
                ["clsName":"WBDiscoverViewController","title":"发现","imageName":"discover"],
                ["clsName":"WBProfileViewController","title":"我","imageName":"profile"]
            ]
            
            var vcArray = [UIViewController]()
            for dict in array {
                vcArray.append(controller(dict: dict))
            }
            viewControllers = vcArray
        }
        
        // 设置撰写按钮
        func setUpComposeBtn() -> () {
            tabBar.addSubview(composeBtn)
            let count = CGFloat(childViewControllers.count)
            // 设置按钮坐标
            // 将缩进宽度w减少点 让按钮的宽度变大 盖住容错点 防止穿帮
            let w = tabBar.bounds.width / count - 1
            // x坐标是tabbar缩进2个item宽度
            composeBtn.frame = tabBar.bounds.insetBy(dx: 2*w, dy: 0)
            // 设置按钮点击监听方法
            composeBtn.addTarget(self, action: #selector(composeBtnStatus), for: .touchUpInside)
        }
        
        /// 使用字典创建控制器
        ///
        /// - Parameter dict: 信息字典[clsName,title,imageName]
        /// - Returns: 子控制器
        private func controller (dict:[String : String]) -> UIViewController {
            //1. 取得字典内容
            guard let clsName = dict["clsName"],
                let title = dict["title"],
                let imageName = dict["imageName"],
                let cls = NSClassFromString(Bundle.main.namespace + "." + clsName) as? UIViewController.Type else {
                    
                    return UIViewController()
            }
            // 2. 创建视图控制器
            // 1> 将clsname转换成cls
            let vc = cls.init()
            vc.title = title
            // 2> 设置图像
            vc.tabBarItem.image = UIImage(named:"tabbar_" + imageName)
            vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
            
            // 3> 设置tabbar标题字体
            vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .highlighted)
            //*** 系统默认的是12号字  需要的话 直接设置Nomal下的字号
            vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 12)], for: .normal)
            let nav = WBNavgationController(rootViewController:vc)
            
            return nav
        }
        
        

}
